<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Sales
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Sales order history block
 *
 * @category   Mage
 * @package    Mage_Sales
 * @author      Magento Core Team <core@magentocommerce.com>
 */

class PLUTUS_Home_Block_Sales_Order_History extends Mage_Sales_Block_Order_History
{

    public function __construct()
    {
        parent::__construct();
        
        $search_order = $this->getRequest()->getParam("search_order");
        $search_from = $this->getRequest()->getParam('search_from');
        $search_to = $this->getRequest()->getParam('search_to');

        
        $orders = $this->getOrders();
        
        
        if(!empty($search_order)){
            
            $orders->addFieldToFilter('increment_id', array('like' =>"%$search_order%" ));
        }
        if(!empty($search_from)&&!empty($search_to)){

            $date_format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
            $search_from = Mage::app()->getLocale()->date($search_from, $date_format, null, false)->toString('yyyy-MM-dd');
            $search_to = Mage::app()->getLocale()->date($search_to, $date_format, null, false)->toString('yyyy-MM-dd');
            
            $orders->addFieldToFilter('created_at', array('gteq'=>$search_from));
            if($search_from!=$search_to){
                $orders->addFieldToFilter('created_at', array('lteq'=>$search_to));
            }
            

        }
        elseif(!empty($search_from)){

            $date_format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
            $search_from = Mage::app()->getLocale()->date($search_from, $date_format, null, false)->toString('yyyy-MM-dd');
            

            $orders->addFieldToFilter('created_at', array('gteq'=>"$search_from" ));
        }
        elseif(!empty($search_to)){
            $date_format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
            $search_to = Mage::app()->getLocale()->date($search_to, $date_format, null, false)->toString('yyyy-MM-dd');
            $orders->addFieldToFilter('created_at', array('lteq'=>"$search_to" ));
        }
        $this->setOrders($orders);
        
    }
    
}
